package cn.com.util;

import java.util.ArrayList;
import java.util.List; 

import org.apache.log4j.lf5.LF5Appender;

import com.ibm.icu.text.DecimalFormat;

public class RedPacketUtil {

//	//最小红包额度 
//	 private static final int MINMONEY = 1; 
//	 //最大红包额度 
//	 private static final int MAXMONEY = 200 * 100; 
	 //每个红包最大是平均值的倍数 
//	 private static final double TIMES = 2.1; 
	  
	 /** 
	 * @param money 
	 * @param count 
	 * @return 
	 * @Author:lulei 
	 * @Description: 拆分红包 
	 */
//	 public static List<Integer> splitRedPackets(int money, int count) { 
//	 if (!isRight(money, count)) { 
//	 return null; 
//	 } 
//	 List<Integer> list = new ArrayList<Integer>(); 
//	 //红包最大金额为平均金额的TIMES倍 
//	 int max = (int) (money * TIMES / count); 
//	 max = max > MAXMONEY ? MAXMONEY : max; 
//	 for (int i = 0; i < count; i++) { 
//	 int one = random(money, MINMONEY, max, count - i); 
//	 list.add(one); 
//	 money -= one; 
//	 } 
//	 return list; 
//	 } 
	 
	 /** 
	 * @param total  红包总金额 
	 * @param count  红包总个数 
	 * @param max	 最大红包金额 
	 * @param min	 最小红包金额
	 * @param usedCount	 已发红包总个数
	 * @param usedMoney	 已发红包总金额
	 * @return 
	 * @Author:henry 
	 * @Description: 随机红包额度 
	 */
	 public static float generate(long total, int count, long MAXMONEY, long MINMONEY, double times) 
	 {
		 if (!isRight(total, count, MINMONEY, MAXMONEY)) { 
			 return 0.0F; 
			 } 
		//红包最大金额为平均金额的TIMES倍 
		long max = (long)(total * times / count); 
		max = max > MAXMONEY ? MAXMONEY : max; 
		
		long one = random(total, MINMONEY, max, count, MINMONEY, MAXMONEY); 
		DecimalFormat df = new DecimalFormat("#.##");
		float retMoney = Float.parseFloat(df.format(Double.valueOf(one)/100)); 
		return retMoney;
	 }
	 
	 /** 
	 * @param money 
	 * @param minS 
	 * @param maxS 
	 * @param count 
	 * @return 
	 * @Author:lulei 
	 * @Description: 随机红包额度 
	 */
	 private static long random(long money, long minS, long maxS, int count, long ora_min, long ora_max) 
	 { 
		 //红包数量为1，直接返回金额 
		 if (count == 1) 
		 { 
			 return money; 
		 } 
		 //如果最大金额和最小金额相等，直接返回金额 
		 if (minS == maxS) { 
			 return minS; 
		 } 
		 long max = maxS > money ? money : maxS; 
		 //随机产生一个红包 
//		 long one = ((int)Math.rint(Math.random() * (max - minS) + minS)) % max; 
		 long one = (int)Math.rint(Math.random() * (max - minS) + minS);
		 long money1 = money - one; 
		 //判断该种分配方案是否正确 
		 if (isRight(money1, count -1, ora_min, ora_max)) { 
			 if(one < ora_min)
			 {
				 System.out.println("");
			 }
			 return one; 
		 } else { 
			 double avg = money1 / (count - 1); 
			 if (avg < ora_min) { 
				 //递归调用，修改红包最大金额 
				 return random(money, minS, one, count, ora_min, ora_max); 
			 }else if (avg > ora_max) 
			 { 
				 //递归调用，修改红包最小金额 
				 return random(money, one, maxS, count, ora_min, ora_max); 
			 } 
		 } 
		 if(one < ora_min)
		 {
			 System.out.println("");
		 }
		 return one; 
	 } 
	  
	 /** 
	 * @param money 
	 * @param count 
	 * @return 
	 * @Author:lulei 
	 * @Description: 此种红包是否合法 
	 */
	 private static boolean isRight(long money, int count, long min, long max) { 
	 long avg = money / count; 
	 if (avg < min) { 
	 return false; 
	 } 
	 if (avg > max) { 
	 return false; 
	 } 
	 return true; 
	 } 
	  
	 public static void main(String[] args) { 
	 // TODO Auto-generated method stub 
//	 RedPacketUtil util = new RedPacketUtil(); 
//	 System.out.println(JsonUtil.parseJson(util.splitRedPackets(20000, 100))); 
	 } 

}
